package com.pubinfo.passbook.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.api.eip.UserInfoService;
import com.pubinfo.passbook.api.service.TFeedbackFileService;
import com.pubinfo.passbook.api.service.TPersonFeedbackService;
import com.pubinfo.passbook.common.entity.TFileInfo;
import com.pubinfo.passbook.common.entity.TPersonFeedback;
import com.pubinfo.passbook.common.mapper.TFeedbackFileMapper;
import com.pubinfo.passbook.common.mapper.TPersonFeedbackMapper;
import com.pubinfo.passbook.common.model.dto.feedback.TPersonFeedbackParam;
import com.pubinfo.passbook.common.model.vo.FeedbackListVo;
import com.pubinfo.passbook.common.model.dto.feedback.FeedbackParam;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xuhong
 * @since 2022-02-28
 */
@Service
public class TPersonFeedbackServiceImpl extends ServiceImpl<TPersonFeedbackMapper, TPersonFeedback> implements TPersonFeedbackService {

    @Autowired
    TFeedbackFileService tFeedbackFileService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    TFeedbackFileMapper tFeedbackFileMapper;

    @Override
    public JsonResult<String> addTPersonFeedback(FeedbackParam feedbackVo) throws UnirestException {
        feedbackVo.getTPersonFeedback().setAcct(userInfoService.getCurrentAcctId());
        feedbackVo.getTPersonFeedback().setState("0");
        feedbackVo.getTPersonFeedback().setTokenInformation(JSONObject.toJSONString(userInfoService.getUserInformation()));
        baseMapper.insert(feedbackVo.getTPersonFeedback());
        if(CollectionUtils.isNotEmpty(feedbackVo.getMultipartFiles()))
        {
            return JSONObject.parseObject(tFeedbackFileService.addFeedbackFile(feedbackVo),JsonResult.class);
        }
        return JsonResult.success("true");
    }

    @Override
    public IPage<TPersonFeedback> selectFeedbackList(TPersonFeedbackParam tPersonFeedbackParam)
    {
        IPage<TFileInfo> iPage = page(tPersonFeedbackParam.getPage());
        return baseMapper.selectFeedbackList(iPage,tPersonFeedbackParam);
    }

    @Override
    public FeedbackListVo selectFeedbackDetailed(String id) {
        TPersonFeedback tPersonFeedback=baseMapper.selectById(id);
        FeedbackListVo feedbackListVo=new FeedbackListVo(tPersonFeedback);
        feedbackListVo.setFeedbackFileList(tFeedbackFileMapper.selectTFeedbackFileByFeedBackId(id));
        return feedbackListVo;
    }

    @Override
    public Integer updateFeedback(String id) {
        TPersonFeedback tPersonFeedback=new TPersonFeedback();
        tPersonFeedback.setId(id);
        tPersonFeedback.setState("1");
        return baseMapper.updateById(tPersonFeedback);
    }

}
